<?php
class IndexApp extends CommonApp {
   
    //学生登录界面
    public function studentAct(){
      self::clear();
      $this->display('Index/index');
    }

    //学生登录界面
    public function teacherAct(){
      self::clear();
      $this->display('Index/index');
    }

   	//登录界面
    public function verfityAct(){
    	// 用户已登陆直接跳转到问卷页面
        // if(My::user('member.memberid') && My::user('member.paper_type'))
        //     redirect(Router::encodeUrl('index.php?app=question&act=index'));
    	if($_POST){
    		//用户名判断
    		if(empty($_POST['username']))
    			$this->ajaxReturn(300,'用户名不能为空！');
    		//密码判断
    		if(!My::is_string($_POST['password']))
    			$this->ajaxReturn(300,'密码不能为空！');
    		//判断验证码
    		$verify = new Verify();
        if(!$verify->check(My::arr2var($_POST,'verify')))
          $this->ajaxReturn(300,'验证码错误!');
        $result = DB('homeuser')->where("username='{$_POST['username']}' AND password='".md5(md5($_POST['password']))."' AND type='".$_POST['type']."'")->cache()->find();
        $type = C('type');
        if(is_array($result)){
            $ok = self::doLogin($result['id'],'homeuser');
            if($result)
                $this->ajaxReturn(200,'登陆成功!!');
            else
                $this->ajaxReturn(300,'登陆失败（设置登录状态时异常）');
        }else
            $this->ajaxReturn(300,'该'.$type[$_POST['type']].'用户名或密码错误!');
    	}
    }
   

    public function student_registerAct(){
      self::clear();
      $this->display('Index/register');
    }

    public function teacher_registerAct(){
      self::clear();
      $this->display('Index/register');
    }

    //注册页面
    public function registerAct(){
    	if($_POST){
    		//用户名
    		if(!My::is_string($_POST['username'],array('c')))
    			$this->ajaxReturn(300,array('message'=>'真实姓名填写有误！','name'=>'username'));
        //用户名
        if(!My::is_email($_POST['email']))
          $this->ajaxReturn(300,array('message'=>'邮箱填写有误！','name'=>'email'));
    		//密码判断
    		if(!My::is_string($_POST['password']))
    			$this->ajaxReturn(300,array('message'=>'密码填写有误！','name'=>'password'));
    		//确认密码判断
    		if(!My::is_string($_POST['s_password']))
    			$this->ajaxReturn(300,array('message'=>'确认密码填写有误！','name'=>'s_password'));
    		if($_POST['s_password'] !== $_POST['password'])
    			$this->ajaxReturn(300,array('message'=>'两次密码输入不一致！','name'=>'s_password'));
    		//判断验证码
    		$verify = new Verify();
        if(!$verify->check(My::arr2var($_POST,'verify')))
            $this->ajaxReturn(300,array('message'=>'验证码错误！','name'=>'verify'));
        $result = DB('homeuser')->where("username='{$_POST['username']}'")->find();
        if($result)
          $this->ajaxReturn(300,array('message'=>'该姓名已存在，请重新填写！','name'=>'username'));
        $_POST['power_1'] = '1';
        $_POST['power_2'] = '1';
        $_POST['password'] = md5(md5($_POST['password']));
        $_POST['login_time'] = date("Y-m-d H:i:s");
        $ok = DB('homeuser')->clear()->add($_POST);
  			if($ok>=0)	$this->ajaxReturn(200,"添加成功");
  			else		$this->ajaxReturn(300,"添加失败");
    	}
    }

  	/**
     * 设置登录状态数据操作
     * @param int $memberid 用户id
     * @param string $login_type 问卷类型
     */
    private function doLogin($memberid, $login_type){
        if(!My::is_id($memberid)) return false;
        //当前登录用户的信息
        $result = DB($login_type)->field('password',true)->where('id='.$memberid)->find();
        if(!is_array($result)) return false;
        //更新当前登录IP/登录时间/登出时间
        DB($login_type)->field('login_ip,login_time')->where("id={$memberid}")->save(array('login_ip'=>get_client_ip(), 'login_time'=>date('Y-m-d H:i:s')));
        //登录次数++
        DB($login_type)->where('id='.$memberid)->setInc('hit');
        // $result['params'] = My::decodeJson($result['params']);
        $result['memberid'] = $memberid;
        //已有memberid，此处去除id
        unset($result['id']);
        //设置前台用户登录session数据
        $_SESSION['member'] = $result;
        return true;
    }

    /**
   	* 用户退出
   	*/
    public function logoutAct() {
      $memberid = My::arr2var($_SESSION,'member.memberid');
      DB('homeuser')->field('loginout_time')->where("id={$memberid}")->save(array('loginout_time'=>date('Y-m-d H:i:s')));
      //删除本系统的session
      session_destroy();
      redirect(Router::encodeUrl('index.php?app=index&act=student'));
    }

    //清除缓存
    function clear(){
      rm_dir(DATA_DIR.'Cache');//cache缓存
    }


   //  //信息填写
   //  public function informationAct(){
   //  	if($_GET)
   //  		$this->assign('type',My::arr2var($_GET,'type'));
   //  	if($_POST) {
   //  		$type = My::arr2var($_POST,'type');
   //  		$params = My::arr2var($_POST,'params');
   //  		//请填写姓名
   //  		if(!My::is_string($_POST['name'],array('c')))
   //  			$this->ajaxReturn(300,'姓名填写有误！');
   //  		//单位判断
   //  		if(empty($params['class'])){
   //  			$error = ($type == '0') ? '班级' : '单位';
   //  			$this->ajaxReturn(300,$error.'填写有误！');
   //  		}
   //  		//出生日期
   //  		if(!My::is_date($params['birthdate']))
   //  			$this->ajaxReturn(300,'出生日期填写有误！');
   //  		if(!My::is_card($params['identification']))
   //  			$this->ajaxReturn(300,'身份证填写有误！');
   //  		//请填写姓名
   //  		if(!My::is_string($params['nation'],array('c')))
   //  			$this->ajaxReturn(300,'民族填写有误！');
   //  		//工号判断
   //  		if(empty($params['number'])){
   //  			$error = ($type == '0') ? '学号' : '工号';
   //  			$this->ajaxReturn(300,$error.'填写有误！');
   //  		}
   //  		$_POST['adddate'] = date("Y-m-d H:i:s");
   //  		$ok = DB($this->table)->clear()->add($_POST);
			// //保存表单模型参数
			// My::setParams($this->table,$params,$ok,$item);
			// if($ok>=0)	$this->ajaxReturn(200,"添加成功");
			// else		$this->ajaxReturn(300,"添加失败");
   //  	}
   //  	$this->display();
   //  }

}